package com.hngy.java;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function2;

import java.util.Arrays;
import java.util.List;

public class CreateRddArrayJava {

    public static void main(String[] args) {
        //创建sparkContext
        SparkConf conf = new SparkConf();
        conf.setAppName("CreateRddArrayJava")
                .setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(conf);
        //创建集合
        List<Integer> arr = Arrays.asList(1,2,3,4,5);
        //基于集合创建RDD
        JavaRDD<Integer> rdd = sc.parallelize(arr);
        Integer num = rdd.reduce(new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer i1, Integer i2) throws Exception {
                return i1 + i2;
            }
        });
        System.out.println(num);
        //停止sparkContext
        sc.stop();

    }
}
